<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>获取/设置响应的 HTTP 状态码</title>
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-base.css" />
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-medium.css" />

 </head>
 <body class="docs"><div class="navbar navbar-fixed-top">
  <div class="navbar-inner clearfix">
    <ul class="nav" style="width: 100%">
      <li style="float: left;"><a href="function.headers-sent.html">« headers_sent</a></li>
      <li style="float: right;"><a href="function.inet-ntop.html">inet_ntop »</a></li>
    </ul>
  </div>
</div>
<div id="breadcrumbs" class="clearfix">
  <ul class="breadcrumbs-container">
    <li><a href="index.html">PHP Manual</a></li>
    <li><a href="ref.network.html">网络 函数</a></li>
    <li>获取/设置响应的 HTTP 状态码</li>
  </ul>
</div>
<div id="layout">
  <div id="layout-content"><div id="function.http-response-code" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">http_response_code</h1>
  <p class="verinfo">(PHP 5 &gt;= 5.4.0, PHP 7, PHP 8)</p><p class="refpurpose"><span class="refname">http_response_code</span> &mdash; <span class="dc-title">获取/设置响应的 HTTP 状态码</span></p>

 </div>

 <div class="refsect1 description" id="refsect1-function.http-response-code-description">
  <h3 class="title">说明</h3>
  <div class="methodsynopsis dc-description">
   <span class="methodname"><strong>http_response_code</strong></span>(<span class="methodparam"><span class="type">int</span> <code class="parameter">$response_code</code><span class="initializer"> = 0</span></span>): <span class="type"><span class="type">int</span>|<span class="type">bool</span></span></div>

  <p class="para rdfs-comment">
   获取或者设置响应的 HTTP 状态码。
  </p>
 </div>


 <div class="refsect1 parameters" id="refsect1-function.http-response-code-parameters">
  <h3 class="title">参数</h3>
  <p class="para">
   <dl>
    
     <dt>
<code class="parameter">response_code</code></dt>

     <dd>

      <p class="para">
       可选的 <code class="parameter">response_code</code> 会设置响应的状态码。
      </p>
     </dd>

       
   </dl>

  </p>
 </div>


 <div class="refsect1 returnvalues" id="refsect1-function.http-response-code-returnvalues">
  <h3 class="title">返回值</h3>
  <p class="para">
   如果提供了 <code class="parameter">response_code</code>，将返回先前的状态码。
   如果未提供 <code class="parameter">response_code</code>，会返回当前的状态码。
   在 Web 服务器环境里，这些状态码的默认值都是 <code class="literal">200</code>。
  </p>
  <p class="para">
   如果在非 Web 服务器环境里调用（比如 CLI 应用里），
   不提供 <code class="parameter">response_code</code> 就会返回 <strong><code>false</code></strong> 。
   在非 Web 服务器环境里，提供 <code class="parameter">response_code</code>  会返回 <strong><code>true</code></strong> 
   （仅仅在先前没有设置过状态码的时候）。
  </p>
 </div>


 <div class="refsect1 examples" id="refsect1-function.http-response-code-examples">
  <h3 class="title">范例</h3>
  <p class="para">
   <div class="example" id="example-4441">
    <p><strong>示例 #1 Web 服务器环境内使用 <span class="function"><strong>http_response_code()</strong></span></strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /><br /></span><span style="color: #FF8000">//&nbsp;获取当前状态码，并设置新的状态码<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">http_response_code</span><span style="color: #007700">(</span><span style="color: #0000BB">404</span><span style="color: #007700">));<br /><br /></span><span style="color: #FF8000">//获取新的状态码<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">http_response_code</span><span style="color: #007700">());<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

    <div class="example-contents"><p>以上例程会输出：</p></div>
    <div class="example-contents screen">
<div class="cdata"><pre>
int(200)
int(404)
</pre></div>
    </div>
   </div>
   <div class="example" id="example-4442">
    <p><strong>示例 #2 在 CLI 环境内使用 <span class="function"><strong>http_response_code()</strong></span></strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /><br /></span><span style="color: #FF8000">//&nbsp;获取当前默认的响应状态码&nbsp;<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">http_response_code</span><span style="color: #007700">());<br /><br /></span><span style="color: #FF8000">//&nbsp;设置状态码<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">http_response_code</span><span style="color: #007700">(</span><span style="color: #0000BB">201</span><span style="color: #007700">));<br /><br /></span><span style="color: #FF8000">//&nbsp;获取新的状态码<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">http_response_code</span><span style="color: #007700">());<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

    <div class="example-contents"><p>以上例程会输出：</p></div>
    <div class="example-contents screen">
<div class="cdata"><pre>
bool(false)
bool(true)
int(201)
</pre></div>
    </div>
   </div>
  </p>
 </div>

 
 <div class="refsect1 seealso" id="refsect1-function.http-response-code-seealso">
  <h3 class="title">参见</h3>
  <p class="para">
   <ul class="simplelist">
    <li class="member"><span class="function"><a href="function.header.html" class="function" rel="rdfs-seeAlso">header()</a> - 发送原生 HTTP 头</span></li>
    <li class="member"><span class="function"><a href="function.headers-list.html" class="function" rel="rdfs-seeAlso">headers_list()</a> - 返回已发送的 HTTP 响应头（或准备发送的）</span></li>
   </ul>
  </p>
 </div>
 


</div></div></div></body></html>